// pages/search/search.js
import {request} from "../../request/request.js";
let regeneratorRuntim = require("../../utils/regenerator-runtime/runtime");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchList:[],
    searchValue:"",
    isBtn:true
  },
// 自定义全局定时器
searchTime:-1,
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },
  searchInput(e){
    let {value} = e.detail;
    if(!value.trim()){
      console.log("为空")
      this.setData({
        searchList:[],
        isBtn:true
      })
      return
    }
    this.setData({
      isBtn:false
    })
    clearTimeout(this.searchTime);
    this.searchTime = setTimeout(()=>{
      this.searchRequest(value)
    },1000)
    
  },
  async searchRequest(query){
    const res = await request({url:"/goods/qsearch",data:{query}});
    this.setData({
      searchList:res.data.message
    })
    console.log(this.data.searchList)
  },
  handBtn(){
    this.setData({
      searchList:[],
      searchValue:"",
      isBtn:true
    })
  }
})